package com.loe.mvp.util;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class Animator implements Animation.AnimationListener
{
    private View view;

    private float pivotX, pivotY;

    private long time = 150;

    private boolean isEnd = false;

    private OnAnimateEndListener onAnimateEndListener;

    private Interpolator interpolator = new LinearInterpolator();

    public Animator(View view)
    {
        setView(view);
    }

    public Animator(View view, long time)
    {
        setView(view);
        setTime(time);
    }

    public Animator(View view, double pivotX, double pivotY)
    {
        setView(view);
        setPivot(pivotX, pivotY);
    }

    public View getView()
    {
        return view;
    }

    public void setView(View view)
    {
        this.view = view;
    }

    public void setPivot(double pivotX, double pivotY)
    {
        this.pivotX = (float) pivotX;
        this.pivotY = (float) pivotY;
    }

    public float getPivotX()
    {
        return pivotX;
    }

    public float getPivotY()
    {
        return pivotY;
    }

    public void setTime(long time)
    {
        this.time = time;
    }

    public long getTime()
    {
        return time;
    }


    public void transAlpha(double fromX, double toX, double fromY, double toY, double fromAlpha, double toAlpha)
    {
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(new AlphaAnimation((float) fromAlpha, (float) toAlpha));
        animationSet.addAnimation(new TranslateAnimation(S, (float) fromX, S, (float) toX, S, (float) fromY, S, (float) toY));
        animationSet.setDuration(time);

        animationSet.setAnimationListener(this);
        animationSet.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animationSet);
    }

    public void transYAlpha(double fromY, double toY, double fromAlpha, double toAlpha)
    {
        transAlpha(0, 0, fromY, toY, fromAlpha, toAlpha);
    }

    public void transXAlpha(double fromX, double toX, double fromAlpha, double toAlpha)
    {
        transAlpha(fromX, toX, 0, 0, fromAlpha, toAlpha);
    }

    public void scale(double fromX, double toX, double fromY, double toY)
    {
        ScaleAnimation animation = new ScaleAnimation((float) fromX, (float) toX, (float) fromY, (float) toY, S, pivotX, S, pivotY);
        animation.setDuration(time);
        animation.setAnimationListener(this);
        animation.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animation);
    }

    public void scaleX(double fromX, double toX)
    {
        scale(fromX, toX, 1, 1);
    }

    public void scaleY(double fromY, double toY)
    {
        scale(1, 1, fromY, toY);
    }

    public void scaleAlpha(double fromX, double toX, double fromY, double toY, double fromAlpha, double toAlpha)
    {
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(new AlphaAnimation((float) fromAlpha, (float) toAlpha));
        animationSet.addAnimation(new ScaleAnimation((float) fromX, (float) toX, (float) fromY, (float) toY, S, pivotX, S, pivotY));
        animationSet.setDuration(time);

        animationSet.setAnimationListener(this);
        animationSet.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animationSet);
    }

    public void alpha(double fromAlpha, double toAlpha)
    {
        AlphaAnimation animation = new AlphaAnimation((float) fromAlpha, (float) toAlpha);
        animation.setDuration(time);
        animation.setAnimationListener(this);
        animation.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animation);
    }

    private static final int S = Animation.RELATIVE_TO_SELF;
    private static final int P = Animation.RELATIVE_TO_PARENT;

    public void trans(double fromX, double toX, double fromY, double toY)
    {
        TranslateAnimation animation = new TranslateAnimation(S, (float) fromX, S, (float) toX, S, (float) fromY, S, (float) toY);
        animation.setDuration(time);
        animation.setAnimationListener(this);
        animation.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animation);
    }

    public void transX(double fromX, double toX)
    {
        trans(fromX, toX, 0, 0);
    }

    public void transY(double fromY, double toY)
    {
        trans(0, 0, fromY, toY);
    }

    public void transParent(double fromX, double toX, double fromY, double toY)
    {
        TranslateAnimation animation = new TranslateAnimation(P, (float) fromX, P, (float) toX, P, (float) fromY, P, (float) toY);
        animation.setDuration(time);
        animation.setAnimationListener(this);
        animation.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animation);
    }

    public void transXParent(double fromX, double toX)
    {
        transParent(fromX, toX, 0, 0);
    }

    public void transYParent(double fromY, double toY)
    {
        transParent(0, 0, fromY, toY);
    }

    public void transAbs(double fromX, double toX, double fromY, double toY)
    {
        TranslateAnimation animation = new TranslateAnimation((float) fromX, (float) toX, (float) fromY, (float) toY);
        animation.setDuration(time);
        animation.setAnimationListener(this);
        animation.setInterpolator(interpolator);
        setVisible(true);
        view.startAnimation(animation);
    }

    public void transXAbs(double fromX, double toX)
    {
        transAbs(fromX, toX, 0, 0);
    }

    public void transYAbs(double fromY, double toY)
    {
        transAbs(0, 0, fromY, toY);
    }

    public void gone()
    {
        view.setVisibility(View.GONE);
    }

    public void end()
    {
        isEnd = true;
    }

    public void setVisible(boolean isVisible)
    {
        view.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onAnimationStart(Animation animation)
    {
        setVisible(true);
    }

    @Override
    public void onAnimationEnd(Animation animation)
    {
        if (isEnd)
        {
            gone();
            isEnd = false;
            if(onAnimateEndListener != null)
            {
                onAnimateEndListener.onEnd();
            }
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation)
    {
    }

    public void setOnAnimateEndListener(OnAnimateEndListener onAnimateEndListener)
    {
        this.onAnimateEndListener = onAnimateEndListener;
    }

    public void setInterpolator(Interpolator interpolator)
    {
        this.interpolator = interpolator;
    }

    public interface OnAnimateEndListener
    {
        void onEnd();
    }
}